# -*- coding: utf-8 -*-
"""
Created on 2015/12/27 11:36
@File: selenium_ex.py
@Author: Liangrong Li
@
"""

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time

# 模拟登陆百度指数
browser = webdriver.Firefox()
print u"浏览器最大化"
browser.maximize_window()  # 浏览器最大化
browser.set_page_load_timeout(20)  # 防止页面没加载完
browser.get("http://index.baidu.com/?tpl=trend&word=abs")

# browser.find_element_by_class_name("BID.popLogin()").click()

email = browser.find_element_by_xpath('//*[@id="TANGRAM_12__userName"]')
email.clear()
email.send_keys("liangyunwuxu")
pwd = browser.find_element_by_xpath('//*[@id="TANGRAM_12__password"]')
pwd.clear()
pwd.send_keys("******")
form = browser.find_element_by_xpath('//*[@id="TANGRAM_12__form"]')
form.submit()
time.sleep(5)
somedom = WebDriverWait(browser, 20).until(lambda brow: brow.find_elements_by_xpath(
    '//*[@id="auto_gsid_5"]/div[2]/table/tbody/tr[2]/td[2]/div/span[1]/span[1]'))
# somedom = WebDriverWait(browser, 20).until(lambda brow: brow.find_elements_by_xpath('//table[@class="mtable profWagv"]/tbody/tr/td/div[@class="lrRadius"]/span[@class="ftlwhf enc2imgVal"]/span[@class="imgval'))
time.sleep(10)
# somedom = WebDriverWait(browser, 10).until(lambda brow: brow.find_elements_by_class_name('ftlwhf enc2imgVal'))
# 用户名 密码
# elem_user = browser.find_element_by_name("userName")
# elem_user.send_keys("liliangrong0@126.com")
# elem_pwd = browser.find_element_by_name("password")
# elem_pwd.send_keys("wuxu139248")
# elem_pwd.send_keys(Keys.RETURN)
# time.sleep(5)
# element = browser.find_element_by_xpath('//*[@id="auto_gsid_5"]/div[2]/table/tbody/tr[2]/td[2]/div/span[1]/span[5]') # get element on page
# print element.text # get element text
try:
    # element = somedom.find_element_by_xpath('/span[@class="ftlwhf enc2imgVal"]') # get element on page
    for i in somedom:
        print i # get element text
    # print somedom.text # get element text
except NoSuchElementException:
    assert 5

browser.quit()
browser.close()
